package com.itheima.reggie.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@RequestMapping("/order")
@RestController
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        log.info("订单数据:{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    //移动端
    @GetMapping("/userPage")
    public R pageQuery(Integer page,Integer pageSize){
        //参数封装
        Page pageInfo = new Page(page, pageSize);
        //封装查询条件
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Orders::getOrderTime);
        qw.eq(Orders::getUserId,BaseContext.getCurrentId());
        orderService.page(pageInfo,qw);

        long total = pageInfo.getTotal();
        List records = pageInfo.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);


        return R.success(map);
    }
    //PC端
    @GetMapping("/page")
    public R PageQuery(Integer page,Integer pageSize,String number,String beginTime,String endTime){

        Page<OrdersDto> pageQuery = orderService.PageQuery(page, pageSize, number, beginTime, endTime);

        return R.success(pageQuery);
    }

    @PutMapping
    public R updateStatus(@RequestBody Orders orders){

        orderService.updateById(orders);

        return R.success("派送成功");
    }

    @PostMapping("/again")
    public R again(@RequestBody Orders orders){

        orderService.again(orders);

        return R.success("操作成功");

    }

}
